package com.gitee.wsl.mathematics.geometry.d2.rect.ext

import com.gitee.wsl.math.Math
import com.gitee.wsl.mathematics.coordinate.d2.Coordinate2
import com.gitee.wsl.mathematics.coordinate.d2.Point2
import com.gitee.wsl.mathematics.geometry.d2.rect.RectangleShape2d
import com.gitee.wsl.mathematics.matrix.mat3.Matrix3
import com.gitee.wsl.mathematics.matrix.mat3.ext.transform
import com.gitee.wsl.mathematics.vector.ext.maxComponents
import com.gitee.wsl.mathematics.vector.ext.minComponents
import com.gitee.wsl.mathematics.vector.vec2.Vector2

fun<N:Number,V: Coordinate2<N, V>,R:RectangleShape2d<N, V,R>> RectangleShape2d<N, V,R>.translated(dx: N, dy: N) =
    copy(
        left  = this.left + dx,
        top   = this.top + dy,
        right = this.right+ dx,
        bottom= this.bottom + dy
    )

fun<N:Number,V: Coordinate2<N, V>,R:RectangleShape2d<N, V,R>> RectangleShape2d<N, V,R>.translated(delta: Vector2<N,*>) =
    copy(
        left  = this.left + delta.x,
        top   = this.top + delta.y,
        right = this.right+ delta.x,
        bottom= this.bottom + delta.y
    )

fun<N:Number,V: Coordinate2<N, V>,R:RectangleShape2d<N, V,R>> RectangleShape2d<N,V,R>.transformed(m: Matrix3<N,*,*>):R{
    val tl = topLeft.transform(m)
    val tr = topRight.transform(m)
    val bl = bottomLeft.transform(m)
    val br = bottomRight.transform(m)
    val min = Math.minComponents(tl, tr, bl, br)
    val max = Math.maxComponents(tl, tr, bl, br)
    return create(min.x, min.y, max.x,max.y)
}